package com.example.dessert.controller.fore;

import com.example.dessert.annotation.LoginCheck;
import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.Favorite;
import com.example.dessert.service.FavoriteService;
import com.example.dessert.util.ResponsePojo;
import com.example.dessert.util.ResultCode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: FavoriteController
 * @Description:
 * @Author li
 * @Date 2022/1/14
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore/favorite")
public class FavoriteController extends BaseController  {

    @Resource
    private FavoriteService favoriteService;

    /**
     * 商品收藏
     */
    @PostMapping("/collect")
    @ResponseBody
    public ResponsePojo collect(HttpSession session, @RequestParam Map<String, Object> map) {

        Object userId = session.getAttribute("userId");
        Long productId = Long.parseLong(map.get("productId").toString());

        //查询是否已经收藏该商品
        Favorite userFavorite= favoriteService.queryFavoriteBuyUserId((Long) userId,productId);
        logger.info("Query whether the user has favorite the product result:{}", userFavorite);

        if(userFavorite != null){
            return ResponsePojo.ok();
        }

        Favorite favorite = new Favorite();
        favorite.setUserId((Long) userId)
                .setProductId(productId);
        Long result =  favoriteService.storeFavorite(favorite);
        logger.info("User favorite product results:{}", result);

        return ResponsePojo.ok();

    }

    /**
     * 取消收藏
     */
    @PostMapping("/cancel")
    @ResponseBody
    public ResponsePojo cancel(HttpSession session, @RequestParam Map<String, Object> map) {

        Object userId = session.getAttribute("userId");
        Long productId = Long.parseLong(map.get("productId").toString());

        //查询是否已经收藏该商品
        Favorite userFavorite= favoriteService.queryFavoriteBuyUserId((Long) userId,productId);
        logger.info("Query whether the user has favorite the product result:{}", userFavorite);

        if(userFavorite == null){
            return ResponsePojo.ok();
        }

        //取消收藏
        int result = favoriteService.cancelFavorite(userFavorite.getId());
        logger.info("Create User Address Information Results:{}", result);

        return ResponsePojo.ok();

    }



    /**
     * 收藏列表
     */
    @GetMapping("/list")
    @LoginCheck
    public String list(HttpSession session, Model model, @RequestParam Map<String, Object> map) {

        logger.info("Query user favorite List request parameters:{}", map);

        int currPage = 1;
        int pageSize = ResultCode.DEFAULT_PAGE_SIZE;

        Object userId = session.getAttribute("userId");

        if (map.get("page") != null) {
            currPage = Integer.parseInt(map.get("page").toString());
        }

        int startPage = (currPage - 1) * pageSize;

        List<Favorite> favorites= favoriteService.queryFavoritePage(startPage, pageSize, (Long) userId);
        logger.info("Query user favorite List results :{}", favorites);


        Long totalCount = favoriteService.queryFavoriteCount((Long) userId);
        long totalPage = (int) Math.ceil((double) totalCount / pageSize);

        logger.info("totalPage:{}", totalPage);

        model.addAttribute("favorites", favorites);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currPage", currPage);

        return "fore/favorite/list";

    }


    /**
     * 收藏删除
     */
    @PostMapping("/delFavorite")
    @ResponseBody
    public ResponsePojo delFavorite(HttpSession session, @RequestParam Map<String, Object> map) {

        logger.info("User deletes favorite parameters based on id:{}", map);

        Object userId = session.getAttribute("userId");
        Long favoriteId = Long.parseLong(map.get("favoriteId").toString());

        int result =  favoriteService.deleteFavoriteBuyId(favoriteId);
        logger.info("User deletes favorite result based on id:{}", result);

        return ResponsePojo.ok();

    }
}
